package com.scaffold.card.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.card.entity.Category;
import com.scaffold.card.service.CategoryService;

/**
 * 目录操作xhtml
 * @author wangjg
 */
@Controller
public class CategoryAction extends BasicBizAction{
	@Autowired
	private CategoryService categoryService;

	/**
	 * 编辑目录
	 * @param id 编号
	 */
	@RequestMapping("/category/edit.xhtml")
	public String edit(ModelMap model, Long id){
		if(id!=null){
			Category category = this.categoryService.getCategory(id);
			this.checkOwner(category);
			model.put("category", category);	
			model.put("_update", "Y");
		}
		return "/category/edit.vm"; 
	}
	
	/**
	 * 保存目录
	 */
	@RequestMapping("/category/save.xhtml")
	public String save(String _update, Long id){
		if(!"Y".equals(_update)){
			Category category = new Category();
			this.bindParams(category, "enabled");
			this.setOwner(category);
			this.categoryService.save(category);
		}else{
			Category category = this.categoryService.getCategory(id);
			this.checkOwner(category);
			this.bindParams(category, "enabled");
			this.categoryService.update(category);
		}
		return "redirect:/category/list.xhtml";
	}
	
	/**
	 * 删除目录
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/category/delete.xhtml")
	public String delete(Long id){
		Category category = this.categoryService.getCategory(id);
		this.checkOwner(category);
		this.categoryService.delete(category);
		return "redirect:/category/list.xhtml";
	}

}
